package ro.ubb.executor.impl;

import java.util.List;

import javax.ejb.EJB;
import javax.ejb.Stateless;

import ro.ubb.dataaccess.entities.Author;
import ro.ubb.dataaccess.entities.Book;
import ro.ubb.dataaccess.exception.MyBbException;
import ro.ubb.dataaccess.service.DataAccessServiceAuthor;
import ro.ubb.dataaccess.service.DataAccessServiceBook;
import ro.ubb.executor.BSException;
import ro.ubb.executor.Executor;

@Stateless(name = "Executor", mappedName = "ejb/Executor")
public class ExecutorEJB implements Executor {

	@EJB
	private DataAccessServiceAuthor accessServiceAuthor;

	@EJB
	private DataAccessServiceBook accessServicBook;

	public List<Book> getAllBooks() {
		return accessServicBook.getAll();
	}

	public Book getBookById(Long id) {
		return accessServicBook.getbyId(id);
	}

	public Author getAuthorByName(String name) throws BSException {
		try {
			return accessServiceAuthor.getByName(name);
		} catch (MyBbException dbe) {
			throw new BSException(dbe.getMessage(), dbe.getCause());
		}
	}

	public void saveAuthor(Author author) {
		accessServiceAuthor.save(author);
	}
}
